#include<bits/stdc++.h>
#define ll long long
using namespace std;

//////////////////////////////////////////////////////
char buf[1<<20],*bg=buf,*ed=buf;
char gc(){
	if(bg==ed)ed=(bg=buf)+fread(buf,1,sizeof(buf),stdin);
	return (ed==bg?EOF:*(bg++));
#define getchar gc
}
template <class T = int>
inline T read() {
    char c=getchar();
    bool sign = (c=='-');
    T x=0;
    while (c<'0'||c>'9')c=getchar(),sign|=(c == '-');
    while ('0'<=c&&c<='9')x=(x<<1)+(x<<3)+(c&15),c=getchar();
    return sign?(~x+1):x;
}
///////////////////////////////////////////////////////

pair<int,int> s[4000005];
int n,k,tot,tong[1000005],cnt=0,ans=INT_MAX;
int main(){
	ios::sync_with_stdio(0);
  	freopen("contest.in","r",stdin);
  	freopen("contest.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++){
		k=read();
		for(int j=1;j<=k;j++)s[++tot].first=read(),s[tot].second=i;
	}
	sort(s+1,s+1+tot);
	for(int r=0,l=1;r<=tot;){
		if(tong[s[++r].second]++==0)cnt++;
		while(tong[s[l].second]>1)tong[s[l++].second]--;
		if(cnt==n){
			ans=min(ans,s[r].first-s[l].first);
		}
	}
	cout<<ans;
}
